import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";

function getPlugins() {
  const plugins = [react(), tsconfigPaths()];
  return plugins;
}

export default defineConfig(({ mode }) => {
  const root = new URL(".", import.meta.url).pathname;
  const env = loadEnv(mode, root, "");
  const proxyTarget = env.VITE_DEV_PROXY || "http://127.0.0.1:8000";

  return {
    plugins: getPlugins(),
    server: {
      host: true,
      port: 5173,
      proxy: {
        // 在本地开发环境将 /api 转发到后端
        "/api": {
          target: proxyTarget,
          changeOrigin: true,
          // 与后端路径一致，无需重写
          rewrite: (path) => path,
        },
      },
    },
  };
});
